import java.util.LinkedList;
import java.util.List;

/**
 * @Author @fiber-cloud
 * @Date 2025/8/3 11:34
 * 简单写一个队列（用单链表实现）
 */
public class MyQueue {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode tail;
    public int usedSize;



    //入队列
    public void offer(int val){
        ListNode node =new ListNode(val);
        if (head == null){
            head = node;
            tail = node;
        }else {
            tail.next = node;
            tail = tail.next;
        }
        usedSize++;
    }


    //出队列
    public int poll(){
        //从头删除
        if (head == null){
            return -1;
        }
        int ret = head.val;
        if (head.next != null){
            head = head.next;
        }else {
            tail = null;
        }
        usedSize--;
        return ret;
    }


    //获取队头元素
    public int peek(){
        if (head == null){
            return -1;
        }
        return head.val;
    }
    //获取队列中有效元素个数
    public int size(){
        return usedSize;
    }


    //检测队列是否为空
    public boolean isEmpty(){
        return usedSize == 0;
    }
}
